package com.yunsc.shop.controller.user;

import com.yunsc.common.utils.PageResult;
import com.yunsc.common.utils.Result;
import com.yunsc.common.utils.SecurityUtil;
import com.yunsc.store.entity.YunscUserBalanceLogEntity;
import com.yunsc.store.entity.YunscUserEntity;
import com.yunsc.store.entity.YunscUserPointsLogEntity;
import com.yunsc.store.service.YunscUserBalanceLogService;
import com.yunsc.store.service.YunscUserPointsLogService;
import com.yunsc.store.service.YunscUserService;
import com.yunsc.system.vo.UserAssetsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description: UserController
 * @date: 2024-05-09 21:31
 */
@Api(value = "用户相关接口", tags = "用户相关接口")
@Slf4j
@RestController()
// @RequestMapping("user")
public class UserController {

    @Resource
    private YunscUserService userService;
    @Resource
    private YunscUserPointsLogService userPointsLogService;
    @Resource
    private YunscUserBalanceLogService userBalanceLogService;

    // user/info
    @ApiOperation(value = "获取用户信息", tags = "获取用户信息")
    @RequestMapping(value = "/user/info", method = RequestMethod.GET)
    public Result<?> getUserInfo() {
        Integer currentUserId = SecurityUtil.getCurrentUserId();
        if (currentUserId == null){
            return Result.error(401, "请先登入");
        }
        YunscUserEntity userInfo = userService.queryUserInfo(currentUserId);
        Map<String, Object> map = new HashMap<>();
        map.put("userInfo", userInfo);
        return Result.ok(map);
    }

    // user/assets
    @ApiOperation(value = "获取用户资产", tags = "获取用户资产")
    @RequestMapping(value = "/user/assets", method = RequestMethod.GET)
    public Result<?> getUserAssets() {
        Integer currentUserId = SecurityUtil.getCurrentUserIdEx();
        UserAssetsVO userAssets = userService.queryUserAssets(currentUserId);
        Map<String, Object> map = new HashMap<>();
        map.put("assets", userAssets);
        return Result.ok(map);
    }

    //  points/log/list&page=1
    @ApiOperation(value = "获取用户积分日志列表", tags = "获取用户积分日志列表")
    @GetMapping("/points/log/list")
    public Result<?> queryUserPointsLogList(@RequestParam(defaultValue = "1") Integer page) {
        Integer userId = SecurityUtil.getCurrentUserId();
        PageResult<YunscUserPointsLogEntity> userPointsLog = userPointsLogService.queryUserPointsLog(userId, page);
        return Result.resultPageList(userPointsLog);
    }

    //  balance/log/list&page=1
    @ApiOperation(value = "获取用户余额日志列表", tags = "获取用户余额日志列表")
    @GetMapping("/balance/log/list")
    public Result<?> queryUserBalanceLogList(@RequestParam(defaultValue = "1") Integer page) {
        Integer userId = SecurityUtil.getCurrentUserId();
        PageResult<YunscUserBalanceLogEntity> userBalanceLog = userBalanceLogService.queryUserBalanceLog(userId, page);
        return Result.resultPageList(userBalanceLog);
    }
    // user/personal
    @ApiOperation(value = "保存用户个人中心信息", tags = "保存用户个人中心信息")
    @PostMapping("/user/personal")
    public Result<?> saveUserPersonal(@RequestBody YunscUserEntity user) {
        userService.updateById(user);
        return Result.ok();
    }


}
